#region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Osh
{
	public static class Utils
	{
		public static string SubstringBefore(string str, char ch)
		{
			if(string.IsNullOrEmpty(str))
				return string.Empty;

			int position = str.IndexOf(ch);
			if(position == -1)
				return string.Empty;

			return str.Substring(0, position);
		}

		public static string SubstringAfter(string str, char ch)
		{
			if(string.IsNullOrEmpty(str))
				return string.Empty;

			int position = str.IndexOf(ch);
			if(position == -1)
				return string.Empty;

			return str.Substring(position+1);
		}

		/// <summary>
		/// Reads stream repeatedly until the requested number of bytes is read or the end of stream is reached.
		/// Returns true if the requested number of bytes is read, otherwise false.
		/// </summary>
		/// <param name="stream"></param>
		/// <param name="buffer"></param>
		/// <param name="offset"></param>
		/// <param name="count"></param>
		/// <returns></returns>
		public static bool ReadStream(Stream stream, byte[] buffer, int offset, int count)
		{
			while(count > 0) 
			{
				// Read may return anything from 0 to count.
				int read = stream.Read(buffer, offset, count);
				// The end of the stream is reached.
				if(read == 0)
					return false;

				offset += read;
				count -= read;
			}
			return true;
		}
	}
}
